<div class="layuimini-main">
    <form class="layui-form" action="" lay-filter="patch">
        <input id="patch_info_id" type="hidden" name="patch_info_id"/>
        <div class="layui-form layuimini-form">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>号码布批量下载</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label ">赛事名称</label>
                <div class="layui-input-block">
                    <input type="text" name="patch_event_name" id="patch_event_name"
                           value="" readonly class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">组别</label>
                <div class="layui-input-block">
                    <select name="patch_group">

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">状态</label>
                <div class="layui-input-block">
                    <select name="patch_status">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">线上/线下</label>
                <div class="layui-input-block">
                    <select name="patch_online">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="patchClick">批量下载</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script>


    layui.use(['form'], function () {

        var form = layui.form,
            $ = layui.$;
        var patch_status = [
            {
                name: '报名通过',
                value: '2'
            }, {
                name: '成绩待审核',
                value: '3'
            }, {
                name: '成绩通过',
                value: '4'
            }
        ];
        var patch_online = [
            {
                name: '全部',
                value: ''
            }, {
                name: '线下',
                value: '0'
            }, {
                name: '线上',
                value: '1'
            }
        ];
        loadIndex = layer.load(1, {
            shade: [0.5, '#fff']
        });
        setTimeout(function () {
            let patch_info_id =  $("input[name=patch_info_id]").val();
            console.log(patch_info_id)
            $.ajax({
                url: "/event/group/list?eventId=" +patch_info_id,
                method: "post",
                contentType: "application/json",
                dataType: "json",
                success: function (res) {
                    let group = [] = res.data;
                    if (group.length !== 0) {
                        let patch_group_dom = $(`select[name=patch_group]`);
                        patch_group_dom.empty();
                        patch_group_dom.append('<option value="">全部</option>');
                        for (let i = 0; i < group.length; i++) {
                            $("select[name=patch_group]").append("<option value=" + group[i].id + ">" + group[i].name + "</option>")
                        }
                    }
                    form.render();
                }
            });
            layer.close(loadIndex);
        },1000)

        for (let i = 0; i < patch_status.length; i++) {
            $("select[name=patch_status]").append("<option value=" + patch_status[i].value + ">" + patch_status[i].name + "</option>")
        }
        for (let i = 0; i < patch_online.length; i++) {
            $("select[name=patch_online]").append("<option value=" + patch_online[i].value + ">" + patch_online[i].name + "</option>")
        }
        form.on('submit(patchClick)', function (data) {
            console.log(data.field);
            loadIndex = layer.load(1, {
                shade: [0.5, '#fff']
            });
            let param = {
                eventId:data.field.patch_info_id,
                status:data.field.patch_status,
                groupId:data.field.patch_group,
                online:data.field.patch_online,
            };

            let xhr = new XMLHttpRequest();
            let url = '/download/allNumberBook/by/eventId/online/status';
            xhr.open('post', url, true);
            xhr.responseType = 'blob';
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    let link = document.createElement('a');
                    let url = URL.createObjectURL(xhr.response);
                    console.log(xhr.response.type)
                    if (xhr.response.type == "application/json"){
                        alert("暂无数据")
                    }else{
                        link.style.display = 'none';
                        link.download = 'numberbook.zip';
                        link.href = url;
                        document.body.appendChild(link);
                        link.click();
                    }
                    layer.close(loadIndex);
                }
            }
            xhr.send(JSON.stringify(param));

            return false;
        });
        form.render();
    })

</script>